layui.use('laydate', function(){
  var laydate = layui.laydate;
  laydate.render({
    elem: '#createTime'
  });
});
layui.use('table', function () {
  var table = layui.table;
  table.render({
    elem: '#tableId'
    , url: '/pb/scenic/action/search'
    , cols: [[
      {checkbox: true, fixed: true}
      ,{field: 'name', title: '景区名称'}
      ,{field: 'status',width:60, title: '状态',templet: '#statusTpl'}
      ,{field: 'pictureUrl', title: '景区图片'}
      ,{field: 'brief', title: '简介'}
      ,{field: 'createTime', title: '创建时间'}
      ,{field: 'updateTime', title: '更新时间'}
      ,{fixed: 'right', width: 178, align: 'center', toolbar: '#barDemo'}
    ]],
    text: {
      none: '暂无相关数据'
    }
    , id: 'tableId'
    , page: true
    , limits: [10, 20, 30]
  });

  //排序
  table.on('sort(user)', function (obj) {
    table.reload('tableId', {
      initSort: obj
      , where: {
        sort: obj.field
        , order: obj.type
      }
    });
  });

  //监听工具条
  table.on('tool(user)', function (obj) {
    var data = obj.data;
    if (obj.event === 'detail') {
    } else if (obj.event === 'del') {
      layer.confirm('真的删除行么', function (index) {

        $.ajax({
          type: "delete",
          url: "/pv/scenic/" + data.id,
          dataType: "json",
          success: function (msg) {
            layer.msg("删除成功！");
            table.reload('tableId', {
              page: {
                curr: 1
              }
            });
          }
        })
      });
    } else if (obj.event === 'edit') {
      layer.open({
        title: '更新景区',
        type: 2,
        maxmin: true,
        shadeClose: true,
        shade: 0.4,
        area: ['500px', '500px'],
        content: './scenic-edit.html',
        success: function (layero, index) {
          var body = layer.getChildFrame('body', index);
          for (key in data) {
            body.contents().find("#L_" + key).val(data[key]);
          }
        },
        end: function () {
          table.reload('tableId', {
            page: {
              curr: 1
            }
          });
        }
      });
    }
  });

  var $ = layui.$, active = {
    reload: function () {
      var name = $('#name');
      var createTime = $('#createTime');
      table.reload('tableId', {
        page: {
          curr: 1
        }, where: {
            "name": name.val(),
            "createTime": createTime.val(),
            "timestamp": new Date().getTime()
        }
      });
    }
  };

  $('.x-body .layui-btn').on('click', function () {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});